﻿Public Class FrmMantUsuarios
    Dim dc_resultado As Long
    Dim dg_resultado As String
    Dim dc_usuario As String
    Dim dg_usuario As String
    Dim dg_password As String
    Dim dg_mail As String
    Dim dm_vigente As String
    Dim dm_evento_load As Boolean
    Private Sub BuscarUsuario()

        If TxtdcUsuario.Text.Trim.Length = 0 Then Exit Sub

        dc_usuario = TxtdcUsuario.Text.Trim.ToUpper
        dc_resultado = ModSeguridad.BuscarUsuario(dc_usuario, dg_usuario, dg_password, dg_mail, dm_vigente, dg_resultado)

        If dc_resultado <> 0 Then
            If dc_resultado = 9999 Then
                FraEncabezado.Enabled = False
                FraDetalle.Enabled = True
                TxtdgUsuario.Focus()
            Else
                MsgBox(dg_resultado, MsgBoxStyle.Information)
                Exit Sub
            End If
        Else
            FraEncabezado.Enabled = False
            FraDetalle.Enabled = True
            Me.TxtdgUsuario.Text = dg_usuario
            Me.TxtdgMail.Text = dg_mail
            Me.TxtdgPassword.Text = Encryptar.DecryptString("", dg_password, ModSeguridad.C_dg_clave, Security.Cryptography.CipherMode.ECB)
            Me.ChkVigente.CheckState = IIf(dm_vigente.Trim.ToUpper = "S", CheckState.Checked, CheckState.Unchecked)
            TxtdgUsuario.Focus()
        End If

    End Sub

    Private Sub nuevo()
        Me.FraEncabezado.Enabled = True
        Me.FraDetalle.Enabled = False
        Me.TxtdcUsuario.Text = ""
        Me.TxtdgUsuario.Text = ""
        Me.TxtdgMail.Text = ""
        Me.TxtdgPassword.Text = ""
        If Not dm_evento_load Then
            TxtdcUsuario.Focus()
        End If
    End Sub

    Private Function ValidaDatos() As Boolean
        ValidaDatos = True

        If TxtdcUsuario.Text.Trim.Length = 0 Then
            ValidaDatos = False
            MsgBox("Debe especificar código usuario", MsgBoxStyle.Information)
            TxtdcUsuario.Focus()
            Exit Function
        End If

        If TxtdgUsuario.Text.Trim.Length = 0 Then
            ValidaDatos = False
            MsgBox("Debe especificar nombre de usuario", MsgBoxStyle.Information)
            TxtdgUsuario.Focus()
            Exit Function
        End If

        If TxtdgPassword.Text.Trim.Length = 0 Then
            ValidaDatos = False
            MsgBox("Debe especificar password", MsgBoxStyle.Information)
            TxtdgPassword.Focus()
            Exit Function
        End If

        If TxtdgMail.Text.Trim.Length = 0 Then
            ValidaDatos = False
            MsgBox("Debe especificar mail", MsgBoxStyle.Information)
            TxtdgMail.Focus()
            Exit Function
        End If

    End Function

    Private Sub CmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSalir.Click
        Me.Close()
    End Sub

    Private Sub TxtdcUsuario_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtdcUsuario.GotFocus
        Me.TxtdcUsuario.SelectionStart = 0
        Me.TxtdcUsuario.SelectionLength = Me.TxtdcUsuario.TextLength
    End Sub

    Private Sub TxtdgUsuario_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtdgUsuario.GotFocus
        Me.TxtdgUsuario.SelectionStart = 0
        Me.TxtdgUsuario.SelectionLength = Me.TxtdgUsuario.TextLength
    End Sub

    Private Sub TxtdgPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtdgPassword.GotFocus
        Me.TxtdgPassword.SelectionStart = 0
        Me.TxtdgPassword.SelectionLength = Me.TxtdgPassword.TextLength
    End Sub

    Private Sub TxtdgMail_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtdgMail.GotFocus
        Me.TxtdgMail.SelectionStart = 0
        Me.TxtdgMail.SelectionLength = Me.TxtdgMail.TextLength
    End Sub

    Private Sub TxtdcUsuario_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtdcUsuario.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Call BuscarUsuario()
        End If
    End Sub

    Private Sub TxtdgUsuario_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtdgUsuario.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub TxtdgPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtdgPassword.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub TxtdgMail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtdgMail.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmdNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNuevo.Click
        Call nuevo()
    End Sub

    Private Sub FrmMantUsuarios_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dm_evento_load = True
        Call Me.nuevo()
        dm_evento_load = False
    End Sub

    Private Sub CmdGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdGrabar.Click
        If Not ValidaDatos() Then Exit Sub

        dc_usuario = TxtdcUsuario.Text.Trim.ToUpper
        dg_usuario = TxtdgUsuario.Text.Trim.ToUpper
        dg_mail = TxtdgMail.Text.Trim.ToUpper
        dg_password = Encryptar.EncryptString("", TxtdgPassword.Text.Trim.ToUpper, ModSeguridad.C_dg_clave, Security.Cryptography.CipherMode.ECB)
        dm_vigente = IIf(ChkVigente.CheckState = CheckState.Checked, "S", "N")

        dc_resultado = ModSeguridad.ModificarUsuario(dc_usuario, dg_usuario, dg_password, dg_mail, dm_vigente, dg_resultado)

        If dc_resultado <> 0 Then
            MsgBox(dg_resultado, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
        Else
            MsgBox("Grabación realizada.", MsgBoxStyle.Information)
            Call nuevo()
        End If

    End Sub

    Private Sub CmdAyuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAyuda.Click

    End Sub
End Class